<?php
# file that will process and save to database meeting with accepted meeting time

# include of site functions
include_once $_SERVER["DOCUMENT_ROOT"].'Meet-n-go/php/includes.php'; 	// contains includes of all files with functions
session_start();					// starts session for current user
c_db_connect(); 					// connect to database

# make sure all data was entered

if(!isset($_POST['time']) || empty($_POST['time'])) i_set_msg_err('Empty suggested time');
else{
	$s_date = c_escape($_POST['time']); // escape before doing something with it
}
if(!isset($_POST['meet_id']) || empty($_POST['meet_id'])) i_set_msg_err('Problem with form');
else{
	$meet_id = c_escape($_POST['meet_id']); // escape before doing something with it
}

if( !has_errors() ){
	
	#gather neccessary data
	$query = "SELECT PLACE, GROUP_ID, DURATION FROM prop_meeting WHERE ID = $meet_id";
	$result = c_db_query($query);
	
	if($result){
		while($row = mysql_fetch_array($result)) 
		{
			$place = $row[0];
			$group_id = $row[1];
			$duration = $row[2];
		}
	}
	$s_time = strtotime($s_date);
	$e_time = $s_time + ($duration * 3600);
	$e_date = date('Y-m-d H:i:s', $e_time);
	
	#create def_meeting
	$query = "INSERT INTO def_meeting (GROUP_ID, PLACE, S_TIME, E_TIME) VALUES ('$group_id', '$place', '$s_date', '$e_date')";
	if(!c_db_query($query)) i_set_msg_err('Error in database'); // problem with database
	
	#delete prop_meeting participants
	$query = "DELETE FROM prop_participants WHERE MEET_ID = $meet_id";
	if(!c_db_query($query)) i_set_msg_err('Error in database'); // problem with database
	
	#delete prop_meeting
	$query = "DELETE FROM prop_meeting WHERE ID = $meet_id";
	if(!c_db_query($query)) i_set_msg_err('Error in database'); // problem with database
	
	// TODO for every participant mark meeting time as busy
	
}

c_db_disconnect(); 					// disconnect from database

# Where to go after processing
if( has_errors() ) 	$page_id = I_GROUP_OVERVIEW . '&meet_id='.$meet_id; // if there are erros, go back to meeting suggestion page
else 				$page_id = I_GROUP_OVERVIEW . '&grp='.$group_id; // if there are no erros, go back to group overview page


# code that makes sure, this page is being redirected from afterwards
echo '<head><meta http-equiv="refresh" content="0; url=../../../index.php?id=' . $page_id . '" /></head>';


?>